<template>
  <card>
    <p slot="title">
        <Icon type="compose"></Icon>
        基本信息
    </p>
    <Form ref="formItem" :model="formItem" :rules="ruleForm" :label-width="120">
        <Row type="flex">
            <Col span="6">
                <FormItem label="品类名称：" prop="name">
                    <Input v-model="formItem.name" size="large" placeholder="请填写内容...">
                        <Icon type="information-circled" slot="prepend"></Icon>
                    </Input>
                </FormItem>
            </Col>
        </Row>
        <Row type="flex">
            <Col span="24">
                <FormItem>
                    <Button @click="save" size="large" type="primary">保存</Button>
                    <Button @click="close" size="large" type="ghost" style="margin-left: 8px">关闭</Button>
                </FormItem>
            </Col>
        </Row>
    </Form>
  </card>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
export default {
    components: {
        iviewTable
    },
    data () {
        return {
            baseURL: '/user/category',
            formItem: {
                id: '',
                name: ''
            },
            ruleForm: {
                name: [
                    { required: true, message: '请填写品类名称', pattern: /.+/, trigger: 'blur' }
                ]
            },
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/category',
                defaultFilters: [
                ]
            },
            dataModel: {
                menuId: 280,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '品类名称',
                        key: 'name',
                        align: 'center'
                    }
                ],
                order: 'id desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            }
        }
    },
    methods: {
        init () {
            // 表单初始化
            this.$bus.fillData(this.formItem, this.$route.query.row)
        },
        save () {
            this.$refs['formItem'].validate((valid) => {
                if (valid) {
                    let url = this.baseURL + '/saveOrUpdate'
                    this.$ajax.post(url, this.formItem).then((response) => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.$bus.alert('success', '保存成功')
                            this.$bus.init(this.formItem)
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                }
            })
        },
        close () {

        }
    },
    beforeRouteEnter (to, from, next) {
        next(vm => {
            vm.init()
        })
    }
}
</script>

<style>

</style>
